<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .box1{width: 100px;height: 100px;background: red;position: absolute;left:0px;top:0;}
    .line{width: 1px;background: black;position: absolute;left:400px;top:0;height: 400px;}
  </style>
</head>
<body>
  <div class="box1"></div>
  <div class="line"></div>
</body>
<script>

  const box1 = document.querySelector(".box1");
  
  box1.onmouseover = function(){
    // move(box1, {
    //   left: 300
    // }, function(){
    //   move(box1, {
    //     top: 300
    //   }, function(){
    //     move(box1, {
    //       left:0
    //     }, function(){
    //       move(box1, {
    //         top:0
    //       })
    //     })
    //   })
    // });

    move(box1,{left:400},()=>{
      box1.style.background = "yellow"
    })
  }

  // 参数1：要操作的元素
  // 参数2：要操作的属性和目标，对象格式
  // 参数3：动画结束后要执行的功能，可传
  function move(ele, obj, cb){
    if(ele.t) return;
    ele.t = setInterval(()=>{
      let state = true;
      for(let key in obj){
        let now = parseInt(getStyle(ele, key));
        let speed = (obj[key] - now) / 10;
        speed = speed < 0 ? Math.floor(speed) : Math.ceil(speed);
        ele.style[key] = now + speed + "px";
        if(now !== obj[key]){
          state = false;
        }
      }
      if(state){
        clearInterval(ele.t);
        ele.t = undefined;
        // 动画结束后，执行传入的功能
        cb && cb();
      }
    }, 30);
  }

  function getStyle(ele, attr){
    if(ele.currentStyle){
      return ele.currentStyle[attr];
    }else{
      return getComputedStyle(ele)[attr];
    }
  }




  
</script>
</html>